package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.ResponseData;
import com.example.demo.entity.SysRole;
import com.example.demo.entity.TbMenu;
import com.example.demo.entity.TreeData;
import com.example.demo.service.TbMenuService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/menu")
@Controller
public class MenuController {
    @Autowired
    private TbMenuService tbMenuService;
    @RequestMapping("/list")
    public  String roleList(Model model){
        System.out.println("==="+tbMenuService.findAll().size());
        List<TbMenu>menuList =tbMenuService.findAll();
        model.addAttribute("menuList",menuList);
        return "views/menu/menuList";
    }
    @RequestMapping("/openMenu")
    public  String openMenu(Model model){
        List<TbMenu>parentList =tbMenuService.findAll();
        model.addAttribute("parentList",parentList);
        return "/views/menu/menu_add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData openRole(@RequestBody JSONObject jsonObject, Model model, HttpServletRequest request, HttpServletResponse rep){


        ResponseData res =new ResponseData();
        try {
            TbMenu tbMenu = (TbMenu) JSONObject.toBean(jsonObject,TbMenu.class);
            tbMenuService.addMenu(tbMenu);
            res.setCode(rep.getStatus());
            res.setMsg("添加成功!!");
            res.setData(new HashMap<>());
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(rep.getStatus());
            res.setMsg("失败!!");
            res.setData(new HashMap<>());
        }
        return res;
    }
}
